package com.itshops.controller;

import com.itshops.entity.Cart;
import com.itshops.entity.User;
import com.itshops.service.CartService;
import com.itshops.service.impl.CartserviceImpl;
import com.itshops.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cart")
public class CartController extends BaseServlet{
    CartService cartService=new CartserviceImpl();
    //购物车添加
    public String create(HttpServletRequest req,HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {

        //判断是否已经登陆
        HttpSession session = req.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            req.setAttribute("msg","添加购物车先登录");
            return Constants.FORWARD+"/login.jsp";
        }
        //获取商品id和用户id
        Integer uId = loginUser.getUid();
        System.out.println(uId);
        String pId = req.getParameter("pId");

        //调用业务逻辑创建购物车
        cartService.createCart(uId,pId);
        //响应
        return Constants.FORWARD+"/cartSuccess.jsp";
    }

    //显示购物车商品
    public String show(HttpServletRequest req,HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        //先判断用户登录
        HttpSession session = req.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            req.setAttribute("msg","查看购物车先登录");
            return Constants.FORWARD+"/login.jsp";
        }
        //获取参数
        Integer uId = loginUser.getUid();
        //调用业务逻辑
        List<Cart> carts =cartService.findAll(uId);
        //响应处理
        req.setAttribute("carts",carts);
        return Constants.FORWARD+"/cart.jsp";
    }

    //删除单个购物车
    public String delete(HttpServletRequest req,HttpServletResponse resp) throws SQLException {
        //获取购物车id
        String cid = req.getParameter("cid");
        //调用业务逻辑
        cartService.deletecartByCid(cid);
        //转发回到商品展示显示在页面
        return Constants.FORWARD+"/cart?method=show";
    }

    //对购物车里的商品进行添加和删除
    public String update(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        //1.获取cid
        String cid = request.getParameter("cid");
        String price = request.getParameter("price"); //商品的单价
        String cnum = request.getParameter("cnum"); //修改后的数量

        //2.调用业务逻辑进行删除
        cartService.updateCartByCid(cid,price,cnum);
        //3.转发到展示的处理方法中
        return Constants.FORWARD + "/cart?method=show";
    }

    //清空购物车里的商品
    public String clear(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        //1.获取uid
        String uid = request.getParameter("uid");
        String price = request.getParameter("price"); //商品的单价
        String cnum = request.getParameter("cnum"); //修改后的数量

        //2.调用业务逻辑进行删除
        cartService.clearCart(uid);
        //3.转发到展示的处理方法中
        return Constants.FORWARD + "/cart?method=show";
    }
}
